/*
 * @Description: 电影实体
 * @Author: fhw
 * @Date: 2024-06-15
 */

import { ApiProperty } from '@nestjs/swagger';
import { BaseEntity } from 'src/common/entities/entities';
import { Column, Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm';
import { TagEntity } from '../../tags/entities/tag.entity';
import { MoviesEpisodeEntity } from '../../movies-episode/entities/movies-episode.entity';
import { CategoryEntity } from '../../category/entities/category.entity';
import { IsEnum, IsNotEmpty } from 'class-validator';
import { FreeState, Status } from 'src/dictionaries/index.enum';



/** 电影实体 */
@Entity('movie')
export class MovieEntity extends BaseEntity {
    /** 名称 */
    @ApiProperty({ example: '电影名称', description: '电影名称' })
    @Column({
        type: 'varchar',
        comment: '名称',
        length: 255,
    })
    name: string;

    /** 剧情图片 */
    @ApiProperty({ example: 'https://example.com/cover.jpg', description: '剧情图片' })
    @Column({
        type: 'text',
        comment: '剧情图片',
    })
    cover: string;

    /** 是否免费 */
    @ApiProperty({
        enum: FreeState,
        example: FreeState.FREE,
        description: '是否免费（1：免费，2：付费）',
    })
    @Column({
        type: 'enum',
        enum: FreeState,
        default: FreeState.FREE,
    })
    free: FreeState;

    /** 是否推荐 */
    @IsEnum([1, 2], { message: '只能传入1, 2' })
    @IsNotEmpty({ message: '是否推荐。2：否，1：是' })
    @ApiProperty({
        type: Number,
        name: 'isRecommend',
        description: '是否推荐。2：否，1：是',
        enum: [1, 2],
    })
    @Column({
        type: 'int',
        comment: '是否推荐。2：否，1：是',
        default: 2,
    })
    isRecommend?: number;

    /** 播放量 */
    @ApiProperty({ example: 1000, description: '播放量' })
    @Column({
        type: 'int',
        comment: '播放量',
        default: 0,
    })
    playCount: number;

    /** 是否上架 */
    @ApiProperty({
        enum: Status,
        example: Status.ENABLED,
        description: '是否上架状态',
    })
    @Column({
        type: 'enum',
        enum: Status,
        default: Status.ENABLED,
    })
    status: Status;

    /** 介绍 */
    @ApiProperty({ example: '电影介绍内容', description: '电影介绍' })
    @Column({
        type: 'text',
        comment: '电影介绍',
        nullable: true,
    })
    introduction: string;
    /** 排序 */
    @ApiProperty({ example: 'sort', description: '排序' })
    @Column({
        type: 'tinyint',
        comment: '排序',
        default: 0,
    })
    sort: number;
    /** 语言 */
    @ApiProperty({ example: '中文', description: '电影语言' })
    @Column({
        type: 'varchar',
        comment: '语言',
    })
    lang: string;

    /** 评分 */
    @ApiProperty({ description: '评分' })
    @Column('float', { comment: '评分', default: 0 })
    rating: number;

    /** 热度 */
    @ApiProperty({ description: '热度' })
    @Column('int', { comment: '热度', default: 0 })
    popularity: number;

    @ApiProperty({ description: '是否为必看剧' })
    @Column('boolean', { comment: '是否为必看剧', default: false })
    isMustWatch: boolean;

    @ApiProperty({ description: '是否为当前热播剧' })
    @Column('boolean', { comment: '是否为当前热播剧', default: false })
    isCurrentlyAiring: boolean;

    @ApiProperty({ description: '是否为精彩剧集' })
    @Column('boolean', { comment: '是否为精彩剧集', default: false })
    isFeatured: boolean;

    /** 标签 */
    @ApiProperty({ description: '电影标签列表' })
    @ManyToMany(() => TagEntity, (tag) => tag.movies, { cascade: true })
    @JoinTable({
        name: 'movie_tags',
        joinColumn: { name: 'movieId', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'tagId', referencedColumnName: 'id' },
    })
    tags: TagEntity[];

    /** 电影类型 */
    @ApiProperty({ description: '电影类型列表', type: () => [CategoryEntity] })
    @ManyToMany(() => CategoryEntity, (genre) => genre.movies)
    @JoinTable({
        name: 'movie_genres',
        joinColumn: { name: 'movieId', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'genreId', referencedColumnName: 'id' },
    })
    category: CategoryEntity[];

    /** 推荐电影 */
    @ApiProperty({ description: '推荐的电影列表' })
    @ManyToMany(() => MovieEntity)
    @JoinTable({
        name: 'movie_recommendations',
        joinColumn: { name: 'movie_id', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'recommended_movie_id', referencedColumnName: 'id' },
    })
    recommendedMovies: MovieEntity[];

    /** 剧集 */
    @ApiProperty({ description: '电影剧集列表' })
    @OneToMany(() => MoviesEpisodeEntity, (episode) => episode.movies)
    episodes: MoviesEpisodeEntity[];
}
